package servicios;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

public class PopUps implements Pantalla{
    static String pref = "";
    private static final int SCREEN_WIDTH = 300;			  // Algunas constantes usadas para ubicar los componentes en el frame.
    private static final int SCREEN_HEIGHT = 130;
    private static final int LEFT_MARGIN = 15;
    private static final int TOP_MARGIN = 15;
    private static final int BUTT_WIDTH = 80;
    private static final int BUTT_HEIGHT = 22;    
    private static final int LABEL_WIDTH = 175;
    private static final int LABEL_HEIGHT = 12;

    private static JButton bAceptar;
    private static JButton bCancelar;
    private static JButton bSalir;
    private static JFrame cuadro;

    public static  void initFrame(String title){
    	cuadro = new JFrame(title);
    	cuadro.setSize(SCREEN_WIDTH, SCREEN_HEIGHT);
        cuadro.setLocationRelativeTo(null); //centrado
        cuadro.setResizable(false);
        cuadro.setVisible(true);
        cuadro.setAlwaysOnTop(true);
    }
    
    public static void mensajeError(String m) {
    	initFrame("Error");
    	JLabel etiqueta = new JLabel(m);
        JLabel imagen = new JLabel();
        bSalir = new JButton("Salir");
        String imgLocation = pref.concat("res/") + "error" + ".png";
        ImageIcon icon = new ImageIcon (imgLocation);
        if (icon != null) {
            imagen.setIcon(new ImageIcon(imgLocation));
        } else {
            imagen.setText("No hay imagen");
        }
        etiqueta.setText(m);
        cuadro.add(etiqueta);
        cuadro.add(imagen);
        cuadro.add(bSalir);
        imagen.setBounds(LEFT_MARGIN, TOP_MARGIN, icon.getIconWidth(), icon.getIconHeight());
        etiqueta.setBounds(imagen.getX() + OFF_L + icon.getIconWidth(), 30, LABEL_WIDTH, LABEL_HEIGHT);
        bSalir.setBounds((SCREEN_WIDTH-BUTT_WIDTH)/ 2, SCREEN_HEIGHT / 2, BUTT_WIDTH, BUTT_HEIGHT );
        bSalir.addActionListener(new BotonAceptarHandler());
    }

    public static void mensajeAdvertencia(String m) {
        initFrame("Advertencia");
        bAceptar = new JButton("Aceptar");
		bCancelar = new JButton("Cancelar");
		JLabel etiqueta = new JLabel(m);
		cuadro.add(bAceptar);
		cuadro.add(bCancelar);
		cuadro.add(etiqueta);        
        int botonX = (SCREEN_WIDTH - (BUTT_WIDTH * 2 + OFF_S)) / 2;
        int botonY = SCREEN_HEIGHT / 2;
        bAceptar.setBounds(botonX, botonY, BUTT_WIDTH, BUTT_HEIGHT );
        bAceptar.addActionListener(new BotonAceptarHandler());
        botonX = (SCREEN_WIDTH + OFF_S )  /2;
        bCancelar.setBounds(botonX, botonY, BUTT_WIDTH, BUTT_HEIGHT );
        bCancelar.addActionListener(new BotonDescartarHandler());
        etiqueta.setBounds(20, 15, LABEL_WIDTH, LABEL_HEIGHT);
    }

    private static class BotonAceptarHandler implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            System.exit(0);
        }
    }

	private static class BotonDescartarHandler implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            
        }
	}

}
